/*
    Copyright Nicolas Ronvel 2007
	gulix33xp@yahoo.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;

namespace BlindShark.Utils
{
	/// <summary>
	/// Provides String functions
	/// </summary>
	public static class StringUtils
	{
		#region String Comparison
		/// <summary>
		/// Get the Levenshtein distance between two strings
		/// </summary>
		/// <param name="s1">String 1</param>
		/// <param name="s2">String 2</param>
		/// <param name="deleteCost">Cost of a delete operation</param>
		/// <param name="insertCost">Cost of an insert operation</param>
		/// <param name="moveCost">Cost of a move operation</param>
		/// <returns>The Levenshtein distance</returns>
		public static int Levenshtein(string s1, string s2, int deleteCost, int insertCost, int moveCost)
		{
			int n=s1.Length;
    		int m=s2.Length;
    		int[,] distance=new int[n + 1, m + 1]; // Resolving Matrix
    		int cost=0;
    		if(n == 0) return m;
    		if(m == 0) return n;
    		// Initializing Matrix 
    		for(int i=0; i <= n; distance[i, 0]=i++);
    		for(int j=0; j <= m; distance[0, j]=j++);
    		// Detection of the minimum distance
    		for(int i=1; i <= n; i++)
    		{
        		for(int j=1; j <= m;j++)
        		{
            		cost=(s2.Substring(j - 1, 1) == s1.Substring(i - 1, 1) ? 0 : 1);
            		distance[i,j]=Math.Min(distance[i - 1, j] + deleteCost,
            		                       Math.Min(distance[i, j - 1] + insertCost,
            		                                distance[i - 1, j - 1] + cost*moveCost));
        		}
    		}
    		return distance[n, m];
		}
		
		/// <summary>
		/// Get the Levenshtein distance between two strings
		/// </summary>
		/// <param name="s1">String 1</param>
		/// <param name="s2">String 2</param>
		/// <returns>The Levenshtein distance</returns>
		public static int Levenshtein(string s1, string s2)
		{
			return Levenshtein(s1,s2,1,1,1);
		}
		#endregion
	
		#region String Format
		/// <summary>
		/// Change the special characters (accent, ...) into the standard characters
		/// </summary>
		/// <param name="input">Input string</param>
		/// <returns>The string with changed characters</returns>
		public static string RemoveSpecialChars(string input)
		{
			byte[] aOctets = System.Text.Encoding.GetEncoding(1251).GetBytes(input);
			return System.Text.Encoding.ASCII.GetString(aOctets);
		}
		
		#endregion
	}
}
